<template>
  <div>
    <el-dialog title="确认订单" :visible.sync="dialogVisible" width="600px" @close="close">
      <el-form
        v-if="detail&&productInfo"
        v-loading="loading"
        label-suffix="："
        label-width="100px"
        size="small"
      >
        <el-form-item label="类型">{{ subTypeOptions[detail.subType] }}</el-form-item>
        <el-form-item label="房型">{{ detail.title }}</el-form-item>
        <el-form-item v-if="detail.subType===0" label="入住时间">
          <span>{{ detail.bizData.beginDay }} ~ {{ detail.bizData.endDay }}&nbsp;&nbsp;</span>
          <span class="app-primary-color">{{ detail.bizData.days }}晚</span>
        </el-form-item>
        <el-form-item v-if="detail.subType===1|| detail.subType===2" label="可用">
          {{ detail.bizData.beginDay }}
          <span
            v-if="productInfo.periodStartTime||productInfo.periodEndTime"
          >{{ productInfo.periodStartTime|minsToHourMins }}~{{ productInfo.periodEndTime|minsToHourMins }}</span>
          <span v-else>00:00~24:00</span>
        </el-form-item>
        <el-form-item
          v-if="detail.subType===1|| detail.subType===2"
          label="时长"
        >{{ detail.bizData.periodDuration/60 }}小时</el-form-item>
        <el-form-item label="数量">{{ detail.bizData.num }}</el-form-item>
        <el-form-item label="到店时间">{{ detail.bizData.beginDay }} {{ detail.bizData.arrivalTime }}</el-form-item>
        <el-form-item label="备注">
          <el-input v-model.trim="orderRemark" maxlength="30" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" :disabled="loading" @click="sumbit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderDetail, changeOrderStatus } from '../../api/record'
export default {
  data() {
    return {
      // 配置
      subTypeOptions: {
        0: '全日房',
        1: '钟点房',
        2: 'KTV房'
      },
      resolve: null,
      reject: null,
      dialogVisible: null,
      loading: false,
      // 数据
      detail: '',
      productInfo: '',
      orderRemark: ''
    }
  },
  methods: {
    open(detail, productInfo = '') {
      this.detail = detail
      // 是否有商品信息
      if (productInfo) {
        this.productInfo = productInfo
        this.show()
      } else {
        this.loading = true
        getOrderDetail({ orderNo: detail.orderNo })
          .then(res => {
            this.productInfo = res.data.product
            this.show()
          })
          .catch(err => {
            this.close()
          })
          .finally(() => {
            this.loading = false
          })
      }
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.orderRemark = ''
      this.loading = false
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    sumbit() {
      const params = {}
      params.orderNo = this.detail.orderNo
      params.status = 1
      params.orderRemark = this.orderRemark
      this.loading = true
      changeOrderStatus(params)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '订单确认成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.resolve && this.resolve()
          this.hide()
        })
        .catch(err => {})
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
